<template>
<div>
  <el-table
    :data="tableData"
    border
    v-loading="dataListLoading"
    style="width: 100%">
    <el-table-column
      prop="name"
      label="标题"
      width="180">
    </el-table-column>
    <el-table-column
      prop="deadline"
      label="截止日期"
      width="180">
    </el-table-column>
    <el-table-column
      prop="content"
      label="内容"
      width="960">
    </el-table-column>
    <el-table-column label="查重" width="80">
      <template slot-scope="scope">
        <el-button type="text" @click="handleCheck(scope.row.id)">查重</el-button>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="300">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleDetail(scope.row.id)">详情</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.row.id)">删除</el-button>
        <el-button
          size="mini"
          @click="handleDownload(scope.row.id)">下载</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    @size-change="sizeChangeHandle"
    @current-change="currentChangeHandle"
    :current-page="pageIndex"
    :page-sizes="[10, 20, 50, 100]"
    :page-size="pageSize"
    :total="totalPage"
    layout="total, sizes, prev, pager, next, jumper">
  </el-pagination>

  <el-dialog
    title="上传文件"
    :close-on-click-modal="false"
    @close="closeHandle"
    :visible.sync="visible">
    <el-upload
      drag
      :action="url"
      :before-upload="beforeUploadHandle"
      :on-success="successHandle"
      multiple
      :file-list="fileList"
      style="text-align: center;">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只支持压缩包格式！</div>
    </el-upload>
  </el-dialog>
</div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'result',
  data () {
    return {
      visible: false,
      url: 'http://127.0.0.1:8091/check/upload',
      fileList: [],
      classname: '',
      path: '',
      id: 0,
      teacherName: '',
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false
    }
  },
  created () {
    this.getWork()
  },
  methods: {
    getWork () {
      this.dataListLoading = true
      axios.get('http://127.0.0.1:8091/admin/getTeacherName/' + this.$store.state.user.name).then(({data}) => {
        this.teacherName = data && data.errCode === 0 ? data.info : this.$store.state.user.name
      }).then(() => {
        var jsonObj = {'teacher_name': this.teacherName, 'page': this.pageIndex, 'limit': this.pageSize}
        axios.post('http://127.0.0.1:8091/work/findWork/', jsonObj).then(({data}) => {
          this.tableData = data && data.errCode === 0 ? data.info : []
          this.totalPage = data && data.errCode === 0 ? data.total : 0
          this.dataListLoading = false
        })
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getWork()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getWork()
    },
    init (id) {
      this.visible = true
    },
    // 上传之前
    beforeUploadHandle (file) {
      // debugger
      if (!file.name.search('.7z') || !file.name.search('.rar') || !file.name.search('.zip')) {
        this.$message.error('只支持压缩包！')
        return false
      }
      this.classname = file.name
    },

    // 上传成功
    successHandle (response, file, fileList) {
      this.fileList = fileList
      if (response && response.errCode === 0) {
        this.visible = false
        this.classname = response.info.classname
        this.path = response.info.path
        var checkObj = {'classname': this.classname, 'path': this.path, 'workId': this.id}
        axios.post('http://127.0.0.1:8091/check', checkObj).then(({data}) => {
          if (data.errCode === 0) {
            this.$message({
              message: '查重成功',
              type: 'success'
            })
          } else {
            this.$message.error('查重失败')
          }
        })
      } else {
        this.$message.error(response.msg)
      }
    },
    // 弹窗关闭时
    closeHandle () {
      this.fileList = []
      this.$emit('refreshDataList')
    },
    handleCheck (id) {
      this.visible = true
      this.id = id
      console.log(id)
    },
    handleDetail (id) {
      this.$router.push({name: 'detail', query: {workId: id}})
    },
    handleDelete (id) {
      axios.delete('http://127.0.0.1:8091/check/' + id).then(({data}) => {
        if (data.errCode === 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          location.reload()
        } else {
          this.$message.error(data.errMsg)
        }
      }).catch()
    },
    handleDownload (id){
      window.location = 'http://127.0.0.1:8091/work/download/' + id
        // axios.get('http://127.0.0.1:8091/work/download/' + id)
    }
  }
}
</script>

<style scoped>

</style>
